package com.admin.socketmultiplayer.activity

import android.app.Activity
import android.app.Application
import android.content.SharedPreferences
import android.os.StrictMode
import cn.bmob.v3.Bmob

/**
 * Created by 吴昶 on 2018/4/25.
 */
class MyApplication:Application(){

    override fun onCreate() {
        super.onCreate()
        Bmob.initialize(applicationContext,"d4d144d16b4d9b7a3ac755308e0d4573")
        initSharedPreferences()
        val builder = StrictMode.VmPolicy.Builder()
        StrictMode.setVmPolicy(builder.build())
        builder.detectFileUriExposure()
    }

    /**
     * 初始化SharedPreferences
     */
    private fun initSharedPreferences() {
        if (sharedPreferences == null) {
            sharedPreferences = applicationContext!!.getSharedPreferences("kotlintest", Activity.MODE_PRIVATE)
        }
    }

    companion object {
        private var sharedPreferences: SharedPreferences? = null
        var activityList= ArrayList<Activity>()
        /**
         * SharedPreferences 存储int值
         * @param key
         * @param value
         */
        fun setSharedPreferencesInteger(key: String, value: Int) {
            val editor = sharedPreferences!!.edit()
            editor.putInt(key, value)
            editor.commit()
        }

        fun getSharedPreferencesInteger(key: String, value: Int): Int {
            return sharedPreferences!!.getInt(key, value)
        }

        /**
         * SharedPreferences 存储字符串
         * @param key
         * @param value
         */
        fun setSharedPreferencesString(key: String, value: String) {
            val editor = sharedPreferences!!.edit()
            editor.putString(key, value)
            editor.commit()
        }

        fun getSharedPreferencesString(key: String, value: String): String? {
            return sharedPreferences!!.getString(key, value)
        }

        /**
         * SharedPreferences 存储boolean值
         * @param key
         * @param value
         */
        fun setSharedPreferencesBoolean(key: String, value: Boolean) {
            val editor = sharedPreferences!!.edit()
            editor.putBoolean(key, value)
            editor.commit()
        }

        /**
         * @param key
         * @param value
         * @return
         */
        fun getSharedPreferencesBoolean(key: String, value: Boolean): Boolean {
            return sharedPreferences!!.getBoolean(key, value)
        }

        fun addActivityManager(activity: Activity){
            activityList.add(activity)
        }

        fun finishAllActivity(){
            (0 until activityList.count()).forEach {
                activityList[it].finish()
            }
        }
    }
}